package cn.yunhe.action;

import cn.yunhe.entity.Admin;
import cn.yunhe.entity.Dept;
import cn.yunhe.entity.Emp;
import cn.yunhe.service.AdminService;
import cn.yunhe.service.DeptService;
import cn.yunhe.service.EmpService;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Map;

@Controller("adminManagerAction")
@Scope("prototype")
@ParentPackage("struts-default")
public class AdminManagerAction extends ActionUtil{
    private Emp emp;
    private Dept dept;
    private Admin admin;

    @Autowired
    @Qualifier("adminService")
    private AdminService adminService;
    @Autowired
    @Qualifier("deptService")
    private DeptService deptService;
    @Autowired
    @Qualifier("empService")
    private EmpService empService;



    @Action(value = "admin_login", results = {
            @Result(name = "success", location = "/manager.jsp"),
            @Result(name = "error", location = "/error.jsp")
    })
    public String login() {
        Map request = getMapRequest();
        Map session = getMapSession();

        if (admin != null) {
            Admin newAdmin = adminService.login(admin);
            if (newAdmin != null) {
                session.put("admin", newAdmin);
                request.put("result", "登录成功!!");
                return SUCCESS;
            } else {
                request.put("result", "用户名或密码错误!!");
                return SUCCESS;
            }
        } else {
            return ERROR;
        }
    }

    @Action(value = "emp_register", results = {
            @Result(name = "success", location = "/reg.jsp")
    })
    public String register() {
        try {
            emp.setAdmin(admin);
            dept.getEmpSet().add(emp); //部门对象中添加员工对象属性
            adminService.register(admin); //存储员工账号信息
            empService.addEmp(emp); //存储员工信息
            deptService.addDept(dept); //存储部门信息
            Map request = getMapRequest();
            request.put("result", "注册成功!!");
            return SUCCESS;
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @Action(value = "admin_logout",results = {
            @Result(name = "success",location = "/reg.jsp")
    })
    public String logout(){
        Map session = getMapSession();
        Admin admin = (Admin) session.get("admin");
        if (admin == null){
            return SUCCESS;
        }else {
            session.put("admin",null);
            return SUCCESS;
        }
    }


    public AdminService getAdminService() {
        return adminService;
    }

    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public Emp getEmp() {
        return emp;
    }

    public void setEmp(Emp emp) {
        this.emp = emp;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public DeptService getDeptService() {
        return deptService;
    }

    public void setDeptService(DeptService deptService) {
        this.deptService = deptService;
    }

    public EmpService getEmpService() {
        return empService;
    }

    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }

}
